﻿using Microsoft.AspNetCore.Mvc;
using Step4.Unit4.ModelBinding.Models;
using System.Text.Encodings.Web;

namespace Step4.Unit4.ModelBinding.Controllers
{
    /// <summary>
    /// 指定绑定来源测试
    /// </summary>
    public class BindController : Controller
    {
        /// <summary>
        /// 模型数据来源 query (从url中拿参数)
        /// [FromQuery]指定来源为query
        /// </summary>
        /// <param name="test"></param>
        /// <returns></returns>
        public IActionResult BindQueryTest([FromQuery] BindTest test)
        {
            return Ok(test);
        }

        /// <summary>
        /// [FromBody]指定绑定来源被body
        /// 正常默认的就是
        /// </summary>
        /// <param name="test"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BindBodyTest([FromBody] BindTest test)
        {
            return Ok(test);
        }

        /// <summary>
        /// [FromForm]也是从Body中拿参数
        /// </summary>
        /// <returns></returns>
        public IActionResult BindFormDataTest([FromForm]BindTest test)
        {
            return Ok(test);
        }

        /*
            [FromBody] 和 [FromForm]都是从正文Body中拿参数，
            FromBody的请求ContentType是Json
            FromForm 是从表表单数据中拿参数的
        */
        [HttpGet]
        public IActionResult BindRouteTest([FromRoute] BindTest test)
        {
            return Ok(test);
        }

        /// <summary>
        /// [FromHeader] 从请求头里拿参数，绑定模型
        /// </summary>
        /// <param name="test"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult BindHeadTest([FromHeader] int Id, [FromHeader] string Name)
        {
            return Ok("绑定成功！");
        }
    }
}
